package com.yxx.controller.user;

import cn.hutool.core.collection.CollectionUtil;
import com.yxx.mapper.AddressBookMapper;
import com.yxx.medicinecommon.context.BaseContext;
import com.yxx.medicinecommon.result.ResponseResult;
import com.yxx.medicinepojo.entity.AddressBook;
import com.yxx.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yuxiangxun
 * @date 2024/9/24
 * @apiNote
*/

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址相关接口")
public class AddressBookController {

    private static final String No_DEFAULT_ADDRESS = "没有默认地址";

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 根据当前微信登录用户id查询其所有地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("登录用户所有地址信息")
    public ResponseResult<List<AddressBook>> userAddressBookList(){
        //获取当前微信登录用户id
        Long wx_user_id = BaseContext.getCurrentId();
        //创建addressBook对象
        AddressBook addressBook = new AddressBook();

        //将当前微信登录用户id封装到addressBook对象的userId属性中
        addressBook.setUserId(wx_user_id);

        //根据address对象动态查询微信登录用户信息中所有地址信息
        List<AddressBook> list = addressBookService.addressBookList(addressBook);
        return ResponseResult.success(list);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public ResponseResult addAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.addAddressBook(addressBook);
        return ResponseResult.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public ResponseResult setDefaultAddress(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddress(addressBook);
        return ResponseResult.success();
    }

    /**
     * 根据地址id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据地址id查询地址")
    public ResponseResult<AddressBook> getAddressBookById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getAddressBookById(id);
        return ResponseResult.success(addressBook);
    }

    /**
     * 修改地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址信息")
    public ResponseResult updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        return ResponseResult.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public ResponseResult deleteAddressBook(Long id){
        addressBookService.deleteAddressBook(id);
        return ResponseResult.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public ResponseResult<AddressBook> getDefaultAddressBookId(){
        //select * from address_book where user_id = ? and is_default = ?
        //创建AddressBook对象
        AddressBook addressBook = new AddressBook();

        //获取当前微信登录用户id
        Long wx_user_id = BaseContext.getCurrentId();

        //将当前登录用户id封装到Address对象
        addressBook.setUserId(wx_user_id);

        //将默认地址1封装到address对象
        addressBook.setIsDefault(1);

        //动态条件查询地址信息（根据当前登录用户id和默认地址）
        List<AddressBook> addressBookList = addressBookService.addressBookList(addressBook);

        //如果list 不为空 list > 0 ，说明有默认地址
        if(CollectionUtil.isNotEmpty(addressBookList) && addressBookList.size() > 0){
            //因为只有一条默认地址，所以只有1条记录
            //获取到这条记录，成功返回
            AddressBook address = addressBookList.get(0);
            return ResponseResult.success(address);
        }

        //list为空，证明这个用户没有设置默认地址，返回错误信息“没有默认地址”
        return ResponseResult.error(No_DEFAULT_ADDRESS);
    }

}
